package com.jn.gsondemo.views;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import com.jn.gsondemo.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OlympicMedalActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_olympic_medal);

        // 假设你已经从某个地方获取了olympic.json的字符串
        String olympicJsonString = getIntent().getStringExtra("olympic_json");

        try {
            JSONObject olympicJson = new JSONObject(olympicJsonString);
            JSONObject result = olympicJson.getJSONObject("result");
            JSONArray medalsList = result.getJSONArray("medalsList");

            List<String> medalItems = new ArrayList<>();
            for (int i = 0; i < medalsList.length(); i++) {
                JSONObject medal = medalsList.getJSONObject(i);
                String item = medal.getString("countryname") + " - 金牌: " + medal.getString("gold") +
                        ", 银牌: " + medal.getString("silver") + ", 铜牌: " + medal.getString("bronze");
                medalItems.add(item);
            }

            ListView medalListView = findViewById(R.id.medal_list);
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, medalItems);
            medalListView.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
